package Tarea2DobleBolaAgregandoNumBolas;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;


import java.awt.Toolkit;

import javax.swing.JPanel;
import javax.swing.JFrame;

public class DosBolas extends JPanel {
	private static int[] X;	
	private static int[] Y;	
	public static int[] Vy;
	public static int[] Vx;
	public static int[] diam; 
	private static Color[] col;
	private static Graphics2D[] gra;
	private static int tamVen=400;
	private static int n=7;
	
	private static DosBolas juego = new DosBolas();
	
	public static void main(String[] args) {
	JFrame JFcuadro = new JFrame("Tarea 2"); //crear objeto y porner titulo
		
	
	
	JFcuadro.add(juego);//agregar el juego (no creado aun)
	JFcuadro.setVisible(true);//set = configurar visibilidad (verdadero)
	JFcuadro.setSize(tamVen,tamVen);//ancho y alto
	JFcuadro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	DefVect();
	
	while (true){
		juego.moverBolita();
		juego.repaint();
		try {
			Thread.sleep(20);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	}	
	
	private static void DefVect() {
		int PosYrad=2;
		int Vel=2;
		int acol=240;
		gra=new Graphics2D[n];
		col=new Color[n];
		X=new int[n];
		Y=new int[n];
		Vx=new int[n];
		Vy=new int[n];
		diam=new int[n];
		for (int i=0;i<n;i++){
			asignar(i,PosYrad+15,PosYrad+32,Vel+2,Vel,acol,(acol-20),(acol-31),PosYrad);
			PosYrad += 10;
			Vel+=1;
			acol-=15;
		}		
	}
	private static void asignar(int i, int j, int k, int l, int m, int o, int p,
			int q, int r) {
		X[i]=j;
		Y[i]=k;
		Vx[i]=l;
		Vy[i]=m;
		col[i]=new Color(o,p,q);
		diam[i]=r;		
	}
public void paint(Graphics g){		
		super.paint(g);		
		for (int i=0;i<n;i++){
			gra[i]=(Graphics2D)g;
			gra[i].setColor(col[i]);			
			gra[i].fillOval(X[i],Y[i],diam[i],diam[i]);	
			}	
		}
	
public void moverBolita(){		
	for (int i=0;i<n;i++){
		if (X[i] >  this.getWidth() - diam[i] )
			Vx[i]=Vx[i]*-1;				
		if (X[i]<0)			
			Vx[i]=Vx[i]*-1;				
		if (Y[i] > this.getHeight()- diam[i])			
			Vy[i]=Vy[i]*-1;			
		if (Y[i]<0)			
			Vy[i]=Vy[i]*-1;			
		
		X[i]+=Vx[i];		
		Y[i]+=Vy[i];		
	}
}
}

